// CAndCPP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <sstream>

using namespace std;

void printLastLines(string const& filename, int k)
{
    ifstream ifs(filename);
    if (!ifs)
    {
        ostringstream stream;
        stream << "Failed to open file: " << filename << endl;
        throw runtime_error(stream.str());
    }

    queue<string> lines;
    string line;
    while (getline(ifs, line))
    {
        while (lines.size() >= k)
        {
            lines.pop();
        }

        lines.push(line);
    }

    while (!lines.empty())  
    { 
        cout << lines.front() << endl;
        lines.pop();
    }
}

void testPrintLines()
{
    //13.1 Write a method to print the last K lines of an input file using C++.
    printLastLines("ReadMe.txt", 10);
}

int _tmain(int argc, _TCHAR* argv[])
{
    testPrintLines();
    cin.get();
	return 0;
}

